package cn.com.pansky.comm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

/**
 * Servlet Filter implementation class PayFilter
 */
public class PayFilter implements Filter {

    /**
     * Default constructor. 
     */
    public PayFilter() {
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String url = req.getRequestURI();
		if(url.equals("/payment") && req.getSession().getAttribute("user") == null) {
			response.setContentType("text/json; charset=UTF-8");
			ServletOutputStream out = response.getOutputStream();
			JSONObject outDatas = new JSONObject();
			outDatas.put("code", 1);
			outDatas.put("msg", "请登录");
			out.write(outDatas.toString().getBytes("UTF-8"));
			out.flush();
			out.close();
		}
		chain.doFilter(req, res);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}
